package com.android.lovingpet.common;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.android.lovingpet.bean.Account;
import com.android.lovingpet.bean.JsonIds;
import com.android.lovingpet.bean.JsonAccount;
import com.android.lovingpet.bean.JsonTeam;
import com.android.lovingpet.bean.Team;
import com.android.lovingpet.util.CallbackStore;
import com.android.lovingpet.util.HttpUtil;
import com.android.lovingpet.util.MyHttp;
import com.android.lovingpet.util.URLStore;
import com.android.lovingpet.util.Utility;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class UserModel {

    private static UserModel instance;
    private UserModel() {}
    public static UserModel getInstance() {
        if (instance == null) {
            synchronized (UserModel.class) {
                if (instance == null) {
                    instance = new UserModel();
                }
            }
        }
        return instance;
    }

    public void getTeamInfo(int id, FileModel.FileBack back) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        MyHttp.getInstance().postAsync(Utility.getJSessionId(), URLStore.TEAM_INFO_GET, params, new CallbackStore.CallbackImpl<FileModel.FileBack>(back) {
            @Override
            public void onResponse(String json) throws JSONException {
                Log.d("组织信息", json);
                JsonTeam jsonTeam = new Gson().fromJson(json, JsonTeam.class);
                Team team = jsonTeam.getTeam();
                Bundle bundle = new Bundle();
                bundle.putParcelable("team", team);
                back.onSuccess(bundle);
                FileModel.getInstance().downloadImage(id, 0, back);
            }
        });
    }

    public void getInfo(int id, final FileModel.FileBack back) {
        HashMap<String, String> params = new HashMap<>();
        params.put("id", id + "");
        HttpUtil.postAsync(Utility.getJSessionId(), URLStore.PERSON_INFO_GET, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.onFailure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("个人信息", str);
                JsonAccount json = new Gson().fromJson(str, JsonAccount.class);
                if (json.getCode() != 0) {
                    back.onFailure(MyHttp.getCodeString(json.getCode()));
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("account", json.getAccount());
                    back.onSuccess(bundle);
                    FileModel.getInstance().downloadImage(id, 1, back);
                }
            }
        });
    }

    public void getInfoList(final List<Integer> ids, final Back back) {
        if (ids == null || ids.isEmpty())
            return;

        for (int id : ids) {
            getInfo(id, new FileModel.FileBack() {
                private Account account;

                @Override
                public void loadSuccess(String url) {
                    account.setHead(url);
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("account", account);
                    back.onSuccess(bundle);
                }

                @Override
                public void onFailure(String meg) {
                    back.onFailure(meg);
                }

                @Override
                public void onSuccess(@Nullable Bundle bundle) {
                    account = bundle.getParcelable("account");
                }
            });
        }
    }

    public static final int TYPE_TEAM = 0;
    public static final int TYPE_PERSON = 1;
    public void getType(int id, Back back) {
        Map<String, String> params = new HashMap<>();
        params.put("id", id + "");
        MyHttp.getInstance().postAsync(Utility.getJSessionId(), URLStore.USER_TYPE, params, new CallbackStore.CallbackImpl<Back>(back) {
            @Override
            public void onResponse(String json) throws JSONException {
                JSONObject object = new JSONObject(json);
                Bundle bundle = new Bundle();
                bundle.putInt("type", object.getInt("type"));
                back.onSuccess(bundle);
            }
        });
    }

    public void getByAddress(HashMap<String,String> params,final Back back)
    {
        MyHttp.getInstance().postAsync(Utility.getJSessionId(), URLStore.SEARCH_ID_ADDRESS, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.onFailure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseData=response.body().string();
                int code=MyHttp.getCode(responseData);
                if(code==0)
                {
                    JsonIds jsonIds=new Gson().fromJson(responseData,JsonIds.class);
                    Bundle bundle=new Bundle();
                    bundle.putIntegerArrayList("ids",jsonIds.getIds());
                    back.onSuccess(bundle);
                }
                else
                {
                    back.onFailure(MyHttp.getCodeString(code));

                }
            }
        });
    }

    public void getByPhone(String phone, final Back back) {
        HashMap<String, String> params = new HashMap<>();
        params.put("phone", phone);
        HttpUtil.postAsync(Utility.getJSessionId(), URLStore.SEARCH_ID_PHONE, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.onFailure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    JSONObject object = new JSONObject(response.body().string());
                    int status = object.getInt("code");
                    if (status != 0) {
                        back.onFailure(MyHttp.getCodeString(status));
                    } else {
                        ArrayList<Integer> ids = new ArrayList<>(1);
                        ids.add(object.getInt("id"));
                        Bundle bundle = new Bundle();
                        bundle.putIntegerArrayList("ids", ids);
                        back.onSuccess(bundle);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getByEmail(String email,final Back back) {
        HashMap<String, String> params = new HashMap<>();
        params.put("email", email);
        HttpUtil.postAsync(Utility.getJSessionId(), URLStore.SEARCH_ID_EMAIL, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.onFailure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    JSONObject object = new JSONObject(response.body().string());
                    int status = object.getInt("code");
                    if (status != 0) {
                        back.onFailure(HttpUtil.getCodeString(status));
                    } else {
                        ArrayList<Integer> ids = new ArrayList<>(1);
                        ids.add(object.getInt("id"));
                        Bundle bundle = new Bundle();
                        bundle.putIntegerArrayList("ids", ids);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getByName(String name,final Back back) {
        Map<String, String> params = new HashMap<>();
        params.put("name", name);
        HttpUtil.postAsync(Utility.getJSessionId(), URLStore.SEARCH_ID_NAME, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.onFailure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                JsonIds json = new Gson().fromJson(response.body().string(), JsonIds.class);
                int status = json.getCode();
                if (status != 0) {
                    back.onFailure(HttpUtil.getCodeString(status));
                } else {
                    ArrayList<Integer> ids = json.getIds();
                    Bundle bundle = new Bundle();
                    bundle.putIntegerArrayList("ids", ids);
                    back.onSuccess(bundle);
                }
            }
        });
    }

    /***
     * @param mode 传0获取粉丝，别的数字获取关注
     */
    public void getFansOrAttentions(int id, final int mode, final Back back) {
        HashMap<String, String> params = new HashMap<>();
        params.put("id", id + "");
        String url = mode == 0 ? URLStore.LIKE_FOLLOW : URLStore.LIKE_GET;
        HttpUtil.postAsync(Utility.getJSessionId(), url, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.onFailure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("用户信息列表", str);
                JsonIds json = new Gson().fromJson(str, JsonIds.class);
                int status = json.getCode();
                if (status != 0) {
                    back.onFailure(HttpUtil.getCodeString(status));
                } else {
                    Bundle bundle = new Bundle();
                    if (mode == 0) {
                        bundle.putIntegerArrayList("ids", json.getFollow());
                    } else {
                        bundle.putIntegerArrayList("ids", json.getLike());
                    }
                    bundle.putInt("mode", mode);
                    back.onSuccess(bundle);
                }
            }
        });
    }
}
